package com.jf.cloud.service.sysmanager.common.taglib.html;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jf.cloud.service.sysmanager.common.taglib.TagService;
import com.jf.cloud.service.sysmanager.common.util.SpringContextUtil;

/**
 * 后台 input type=button 标签
 * 统一处理后台业务
 */
public class BackButtonTag extends TagSupport {

	private  static final long serialVersionUID = 1L;
	private  static String PUB_KEY="1";
	
	static Log log = LogFactory.getLog(BackButtonTag.class);
	
	protected String id;
	protected String name;
	protected String style;
	protected String value;
	protected String onclick;
	protected String type; // 按钮的类型
	protected String rightKey; //  处理权限
	protected String _class;   //  样式
	protected String cssClass;   //  样式 兼容
	
	/**
	 * 开始
	 * 如果 按钮的 rightKey=1 或者 display 时 默认为与业务无关 直接显示 
	 */
	public int doStartTag() throws JspException {
		try {
			JspWriter writer = pageContext.getOut();
			StringBuffer sfBuffer = new StringBuffer(100);
			String title="title='普通按钮'";
			TagService tagService = null;
			if(StringUtils.isEmpty(type)){ //设置默认值
                  type = "button";
			}
			if(rightKey!=null && !PUB_KEY.equals(rightKey)){ // 不是权限按钮就不实例化
				tagService = SpringContextUtil.getBean(TagService.class);
				String menuDesc = tagService.getMenuDescByKey(rightKey);
				if( StringUtils.isNotBlank(menuDesc)){ 
					title=" title='ID:"+rightKey+"_"+menuDesc+" '" ;
				}else {
					title=" title='ID:"+rightKey+"'";
				}
			}
			sfBuffer.append("<"+type+" ");
			if (id != null) {
				sfBuffer.append(" id=\"" + id + "\"");
			}
			if (name != null) {
				sfBuffer.append(" name=\"" + name + "\"");
			}
			if (style != null) {
				sfBuffer.append(" style=\"" + style + "\"");
			}
			if (onclick != null) {
				sfBuffer.append(" onclick=\"" + onclick + "\"");
			}
			if (value != null) {
				sfBuffer.append(" value=\"" + value + "\"");
			}
			if (_class != null) {
				sfBuffer.append(" class=\"" + _class + "\"");
			}else if(_class == null && cssClass!=null){
				sfBuffer.append(" class=\"" + cssClass + "\"");
			}
			sfBuffer.append(title);
			sfBuffer.append(" type=\""+type+"\"" );
			sfBuffer.append(" >");
			
			if(rightKey!=null && PUB_KEY.equals(rightKey) ){
				writer.println(sfBuffer.toString());
				return EVAL_BODY_INCLUDE;
			}else if (rightKey!=null) {
				HttpServletRequest request = ((HttpServletRequest) pageContext.getRequest());
				if(request!=null){
					if(tagService.checkPowerByUserAndRightKey(request,rightKey)){
			          	writer.println(sfBuffer.toString());
						return EVAL_BODY_INCLUDE; 
					}else {
						log.info("当前 登录人 不拥有此项权限 rightKey="+rightKey);
						return SKIP_BODY; 
					}
				}
			}
			
		} catch (Exception ex) {
			log.error("后台 input type=button 标签", ex);
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		// 结束标签
		StringBuffer sb = new StringBuffer();
		sb.append("</"+type+">");
		try {
			writer.print(sb.toString());
		} catch (IOException ex) {
			throw new JspException("标签HTML输出异常！", ex);
		}
		return EVAL_PAGE;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRightKey() {
		return rightKey;
	}

	public void setRightKey(String rightKey) {
		this.rightKey = rightKey;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String get_class() {
		return _class;
	}

	public void set_class(String _class) {
		this._class = _class;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}
	
	

}
